package com.jiang.starter.swagger;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@Import({SwaggerConsole.class})
@EnableOpenApi
public class SwaggerAutoConfiguration {

    @Value("${spring.application.name: }")
    private String applicationName;
    @Value("${swagger.contact.name: }")
    private String contactName;
    @Value("${swagger.contact.url: }")
    private String contactUrl;
    @Value("${swagger.contact.email: }")
    private String contactEmail;
    @Value("${swagger.terms-service-url: }")
    private String termsOfServiceUrl;
    @Value("${swagger.version: }")
    private String version;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                        .title(applicationName)
                        .description(applicationName + "接口文档。")
                        .contact(new Contact(contactName, contactUrl, contactEmail))
                        .termsOfServiceUrl(termsOfServiceUrl)
                        .version(version)
                        .build())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

}
